<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>37.Key 的作用</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="app"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js2/react.development.js"></script>
    <!-- 引入react-dom 用于支持 react 操作 dom -->
    <script type="text/javascript" src="../js2/react-dom.development.js"></script>
    <!-- 引入 label，用于将 jsx 转化为 js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入 prop-types，用于对于组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>


    <!-- 此处必须写 text/babel -->
    <script type="text/babel">

        // 创建组件
        class Person extends React.Component {

            state = {
                persons: [
                    { id: 1, name: '王肖杰', age: 12 },
                    { id: 2, name: '王肖洛', age: 10 },
                    { id: 3, name: '王俊天', age: 9 }
                ]
            }

            add = () => {
                const { persons } = this.state
                const p = { id: persons.length + 1, name: '王俊宇', age: 4 }
                this.setState({ persons: [p, ...persons] })
            }


            // 初始化渲染，页面更新时候调用
            render() {
                return (
                    <div>
                        <h2>展示人员信息</h2>
                        <button onClick={this.add}>展示一个小王</button>
                        <ul>
                            {
                                this.state.persons.map((personObj, index) => {
                                    return <li key={index}>{personObj.name} --- {personObj.age}</li>
                                })
                            }
                        </ul>
                    </div>
                )
            }
        }

        // 渲染组件
        ReactDOM.render(<Person />, document.getElementById("app"))
    </script>

</body>

</html>